package com.zit.java.controller;

import cn.hutool.core.date.DateUtil;
import com.zit.java.pojo.ReimburseMsg;
import com.zit.java.service.reimburse.ReimburseSerivce;
import com.zit.java.util.object.AjaxDone;
import com.zit.java.util.object.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("reimburse")
public class ReimburseController {

    @Autowired
    private ReimburseSerivce serivce;

    /**
     * 到报销信息页面
     * @return
     */
    @RequestMapping("toReimburse")
    private  String toReimburse(){
        return "reimbuse/index";
    }

    /**
     * 保存报销信息 已解决
     * @param
     * @return
     */
    @RequestMapping("saveReimburse")
    @ResponseBody
    public AjaxDone saveReimburse(
            @RequestParam("reimburseContact") String reimburseContact,
            @RequestParam("reimburseReason") String reimburseReason,
            @RequestParam("reimburseRemark") String reimburseRemark,
            @RequestParam("reimburseDate") String reimburseDate,
            @RequestParam("reimburseManager") String reimburseManager
                                  ){
        //将前台获取的数据封装对象
        ReimburseMsg reimburseMsg = new ReimburseMsg();
        reimburseMsg.setReimburseContact(reimburseContact);
        reimburseMsg.setReimburseDate(DateUtil.parseDate(reimburseDate));
        reimburseMsg.setReimburseReason(reimburseReason);
        reimburseMsg.setReimburseManager(reimburseManager);
        reimburseMsg.setReimburseRemark(reimburseRemark);

       int rst =  serivce.insertData(reimburseMsg);
        if (rst > 0) {
            return new AjaxDone(200, "保存成功");
        }
             return new AjaxDone(300, "保存失败");
    }


    /**
     * 到报销页面
     */

    @RequestMapping("toShowReimburse")

    public String toShouReimburse(){

        return "reimbuse/showreimbuse";
    }

    /**
     * 到上传页面
     * @return
     */
    @RequestMapping("toUploadPag")
    public String toUploadPag(){
        return "reimbuse/upload";
    }

    /**
     * 下载页面 未写完
     * @param filename
     * @return
     */
    @RequestMapping("upload")
    public Map<String,String> uploadImage(@RequestParam("filename") String filename){
        System.out.println(filename);
        return null;
    }

    /**
     * 查看报销所有数据
     * @return
     */
    @RequestMapping("showReimburse")
    @ResponseBody
    public TableData<ReimburseMsg> showReimburse(){
        List<ReimburseMsg> reimburseMsgList = serivce.queryAllReimburse();
        TableData<ReimburseMsg> tableData = new TableData<>();
        tableData.setMsg("成功");
        tableData.setCode(0);
        tableData.setData(reimburseMsgList);
        tableData.setCount(reimburseMsgList.size());
        return tableData;
    }

    /***
     * 通过名字或者日期查询数据
     * @param name
     * @param date
     * @return
     */

    @RequestMapping("showByName")
    @ResponseBody
    public TableData<ReimburseMsg> showByName(@RequestParam("username") String name,@RequestParam("date")String date){
        TableData<ReimburseMsg> tableData = new TableData<>();
        List<ReimburseMsg> reimburseMsgList = serivce.queryByNameAndDate(name,date);
        tableData.setData(reimburseMsgList);
        tableData.setCode(0);
        tableData.setMsg("成功");
        return tableData;
    }
}
